﻿using JinJun.YShow.BLL;
using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.YShow.Web.Controllers
{
    public class HomeController : BaseController
    {
        private ICategoryService _categoryService;
        private IProductService _productService;
        private IPictureService _pictureService;
        private IBrandService _brandService;
        private IOrderService _orderService;



        public HomeController(ICategoryService categoryService, IProductService productService, IPictureService pictureService, IBrandService brandService, IOrderService orderService)
        {
            _categoryService = categoryService;
            _productService = productService;
            _pictureService = pictureService;
            _brandService = brandService;
            _orderService = orderService;
        }

        public HomeController(IUserInfoService userInfoService)
        {
            _userInfoService = userInfoService;
        }

        /// <summary>
        /// 首页镶嵌
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {           
            return View();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Shops_Index()
        {           
            return View();
        }

        /// <summary>
        /// 登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 会员管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Member_List()
        {
            
            return View();
        }

        /// <summary>
        /// 添加商品页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Add_Product()
        {
            BaseService<Spec> baseService = new BaseService<Spec>();
            BaseService<Spec_Value> service = new BaseService<Spec_Value>();
            Expression<Func<Spec, bool>> lambdaCategory = a =>true;
            ViewBag.spec = baseService.QueryList(lambdaCategory);
            Expression<Func<Spec_Value, bool>> lambdaSpecValue = a => true;
            ViewBag.spceValue = service.QueryList(lambdaSpecValue);
            Expression<Func<Category, bool>> lambdacate = a => a.ParentId==0;
            ViewBag.first = _categoryService.QueryList(lambdacate);
            Expression<Func<Brand, bool>> lambdabrand = a => true;
            ViewBag.brand=_brandService.QueryList(lambdabrand);
            return View();
        }
        
        /// <summary>
        /// 商品分类页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Products()
        {
            return View();
        }

        /// <summary>
        /// 编辑商品页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit_Products(int id)
        {
            BaseService<Spec> baseService = new BaseService<Spec>();
            BaseService<Spec_Value> service = new BaseService<Spec_Value>();
            Expression<Func<Spec, bool>> lambdaCategory = a => true;
            ViewBag.spec = baseService.QueryList(lambdaCategory);
            Expression<Func<Spec_Value, bool>> lambdaSpecValue = a => true;
            ViewBag.spceValue = service.QueryList(lambdaSpecValue);
            Expression<Func<Category, bool>> lambdacate = a => a.ParentId == 0;
            ViewBag.first = _categoryService.QueryList(lambdacate);
            Expression<Func<Brand, bool>> lambdabrand = a => true;
            ViewBag.brand = _brandService.QueryList(lambdabrand);
            //表达式
            Expression<Func<Product_Spu, bool>> ProductSpuLambda = a => a.Id == id;
            ViewBag.Product = _productService.Query(ProductSpuLambda);

            BaseService<Produc_Spu_Spec> baseService2 = new BaseService<Produc_Spu_Spec>();
            Expression<Func<Produc_Spu_Spec, bool>> lambdaProduc_Spu_Spec = a => a.Produc_Spu_Id==id;
            ViewBag.SpuSpec = baseService2.QueryList(lambdaProduc_Spu_Spec);
            return View();
        }

        /// <summary>
        /// 品牌管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Brand_Manage()
        {
            return View();
        }


        /// <summary>
        ///交易统计页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Order()
        {
            //表达式
            Expression<Func<OrderInfo, bool>> ids = a =>true;
            //调用查询方法返回一个集合
            List<OrderInfo> orderInfos = _orderService.QueryList(ids);
            decimal count=0;
            //循环遍历拿到交易金额
            for (int i = 0; i < orderInfos.Count; i++)
            {
                count += orderInfos[i].Order_Money;
            }
            ViewBag.Order_Money = count;
            ViewBag.Count = orderInfos.Count;
            return View();
        }



        /// <summary>
        ///订单管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Order_Form()
        {
            //Expression<Func<Category, bool>> Categorylambda = a => a.ParentId == 0;
            List<OrderType> orderTypeslist = new List<OrderType>();
            orderTypeslist = _orderService.OrderType();
            int count = 0;
            foreach (var item in orderTypeslist)
            {
                count += item.Count;
            }
            foreach (var item in orderTypeslist)
            {
                if (item.Count != 0)
                {
                    item.Count = Convert.ToInt32((item.Count * 100) / count);
                }
            }
            //百分比
            ViewBag.OrderType = orderTypeslist;
            //菜单
            List<OrderType> orderTypes = _orderService.OrderType();
            List<OrderType> menuInfo = _orderService.MenuInfo();
            string number = "[";
            for (int i = 0; i < orderTypes.Count; i++)
            {
                if (i + 1 < orderTypes.Count)
                {
                    number += "{id:" + orderTypes[i].Id + ",name:\"" + orderTypes[i].CategoryName + "\",amount:" + orderTypes[i].Count + ",pid:" + orderTypes[i].ParentId + "},";
                    List<OrderType> List = menuInfo.Where(a => a.ParentId == orderTypes[i].Id).ToList();
                    for (int j = 0; j < List.Count; j++)
                    {
                        number += "{id:" + List[j].Id + ",name:\"" + List[j].CategoryName + "\",amount:" + List[j].Count + ",pid:" + List[j].ParentId + "},";
                        //if (j <List.Count)
                        //{
                        //    shuzhi += "{id:" + List[j].Id + ",name:" + List[j].Name + ",amount:" + List[j].Count + ",pid:" + List[j].ParentId + ",},";
                        //}
                        //else
                        //{
                        //    shuzhi += "{id:" + List[j].Id + ",name:" + List[j].Name + ",amount:" + List[j].Count + ",pid:" + List[j].ParentId + ",},";
                        //}
                    }
                }
                else
                {
                    number += "{id:" + orderTypes[i].Id + ",name:\"" + orderTypes[i].CategoryName + "\",amount:" + orderTypes[i].Count + ",pid:" + orderTypes[i].ParentId + "}]";
                }
            }
            ViewBag.number = number;
            return View();           
        }



        /// <summary>
        ///分类管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Category()
        {
            return View();
        }

        /// <summary>
        ///添加分类页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Add_Category()
        {
            return View();
        }

        /// <summary>
        ///编辑分类页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit_Category(int id)
        {
            Expression<Func<Category, bool>> lambdaCategory = a => a.Id == id;
            ViewBag.category = _categoryService.Query(lambdaCategory);
            return View();
        }

        /// <summary>
        ///图片管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Picture()
        {

            return View();
        }

        /// <summary>
        ///添加图片页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Add_Picture()
        {
            Expression<Func<Product_Spu, bool>> lambdaproduct = a => true;
            ViewBag.product = _productService.QueryList(lambdaproduct);
            return View();
        }

        /// <summary>
        ///编辑分类页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit_Picture(int id)
        {
            Expression<Func<Product_Spu, bool>> lambdaproduct = a => true;
            ViewBag.product = _productService.QueryList(lambdaproduct);
            Expression<Func<Pic, bool>> lambdaCategory = a => a.Pic_Id == id;
            ViewBag.pic = _pictureService.Query(lambdaCategory);
            return View();
        }


        /// <summary>
        /// 添加品牌页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Add_Brand()
        {
            return View();
        }


        /// <summary>
        /// 编辑品牌页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Brand_Edit(int id)
        {
            //表达式
            Expression<Func<Brand, bool>> ids = a => a.Id ==id;
            //调用查询方法
            ViewBag.brandInfo =_brandService.Query(ids);
            return View();
        }

        
        /// <summary>
        /// 设置库存页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit_Stock(int id)
        {
            BaseService<Spec> baseService1 = new BaseService<Spec>();
            BaseService<Produc_Spu_Spec> baseService = new BaseService<Produc_Spu_Spec>();
            //表达式
            Expression<Func<Produc_Spu_Spec, bool>> SpecLambda = a => a.Produc_Spu_Id == id;
            //表达式
            Expression<Func<Product_Spu, bool>> ProductSpuLambda = a => a.Id == id;
            //调用查询方法
            List<Produc_Spu_Spec> produc_Spus = baseService.QueryList(SpecLambda);
            List<Spec> specs = new List<Spec>();
            for (int i = 0; i < produc_Spus.Count; i++)
            {
                int spid = produc_Spus[i].Spec_Id;
                //表达式
                Expression<Func<Spec, bool>> SpeLambda = a => a.Id == spid;
                specs.Add(baseService1.Query(SpeLambda));
            }
            ViewBag.Spec = specs;
            ViewBag.Product = _productService.Query(ProductSpuLambda);
            return View();
        }

        /// <summary>
        /// 编辑订单页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Order_Edit(int id)
        {
            //创建业务层对象
            BaseService<OrderInfo> _orderinfo = new BaseService<OrderInfo>();
            BaseService<Order_Detail> _orderdetail = new BaseService<Order_Detail>();
            OrderQuery _orderquery = new OrderQuery();
            //表达式
            Expression<Func<OrderInfo, bool>> orderinfoexpression = a => a.Order_Id == id;
            Expression<Func<Order_Detail, bool>> orderdetailexpression = a => a.Order_Id == id;
            //调用查询方法
            OrderInfo orderInfo = _orderinfo.Query(orderinfoexpression);
            Order_Detail order_Detail = _orderdetail.Query(orderdetailexpression);
            //将查询到的值赋给自定义实体类中的值
            _orderquery.Order_Id = orderInfo.Order_Id;
            _orderquery.Order_No = orderInfo.Order_No;
            _orderquery.Product_Name = order_Detail.Product_Name;
            _orderquery.Order_Money = orderInfo.Order_Money;
            _orderquery.District_Money = orderInfo.District_Money;
            _orderquery.CreateTime = orderInfo.CreateTime;
            _orderquery.Order_Status = orderInfo.Order_Status;
            _orderquery.Product_Cnt =order_Detail.Product_Cnt;
            //ViewBag接收
            ViewBag.OrderInfo = _orderquery;
            return View();
        }


    }
}